package com.woniu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.ChargingCard;
import com.woniu.mapper.ChargingCardMapper;
import com.woniu.service.ChargingCardService;
import com.woniu.vo.ChargingCardVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Apricity
* @description 针对表【charging_card(充电卡表)】的数据库操作Service实现
* @createDate 2025-02-13 20:16:58
*/
@Service
public class ChargingCardServiceImpl extends ServiceImpl<ChargingCardMapper, ChargingCard>
    implements ChargingCardService {

    @Resource
    private ChargingCardMapper chargingCardMapper;

    @Override
    public List<ChargingCardVO> getChargingCardList(String keyword, boolean exact, Integer pageNum, Integer pageSize) {
        Page<ChargingCardVO> page = new Page<>(pageNum, pageSize);
        List<ChargingCardVO> cardList;
        
        if (StringUtils.isNotBlank(keyword)) {
            if (exact) {
                cardList = chargingCardMapper.queryCardWithCustomerExact(keyword, page);
            } else {
                cardList = chargingCardMapper.queryCardWithCustomerByKeyword(keyword, page);
            }
        } else {
            cardList = chargingCardMapper.queryCardWithCustomer(page);
        }
        
        return cardList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addChargingCard(ChargingCard chargingCard) {
        return chargingCardMapper.insert(chargingCard) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteChargingCard(Long id) {
        return chargingCardMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateChargingCard(ChargingCard chargingCard) {
        if (chargingCard.getId() == null) {
            return false;
        }
        return chargingCardMapper.updateChargingCard(chargingCard) > 0;
    }
}




